package com.example.coffeejava.Service.Impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.coffeejava.Dto.InfoDto;
import com.example.coffeejava.Dto.PageResult;
import com.example.coffeejava.Dto.Result;
import com.example.coffeejava.Entity.Info;
import com.example.coffeejava.Entity.Type;
import com.example.coffeejava.Mapper.InfoMapper;
import com.example.coffeejava.Mapper.TypeMapper;
import com.example.coffeejava.Service.InfoService;
import com.example.coffeejava.Service.TypeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.example.coffeejava.utils.Constants.OFF_STATUS;
import static com.example.coffeejava.utils.Constants.UP_STATUS;

@Service
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {
    @Resource
    private InfoMapper infoMapper;

    public PageResult getlist(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Info> page = infoMapper.getlist(keyword);
        PageResult pageResult = new PageResult();
        pageResult.setTotal((int) page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    public Result addorUpdate(InfoDto infoDto) {
        if (infoDto.getId() == null) {
            Info info = new Info();
            info.setIcon(infoDto.getIcon());
            info.setIntro(infoDto.getIntro());
            info.setBuyNum(0);
            info.setWatchNum(0);
            info.setName(infoDto.getName());
            info.setStatus(UP_STATUS);
            info.setOrigin(infoDto.getOrigin());
            info.setTypeId(infoDto.getTypeId());
            save(info);

        } else {
            Info info = new Info();
            info.setIcon(infoDto.getIcon());
            info.setIntro(infoDto.getIntro());
            info.setBuyNum(0);
            info.setWatchNum(0);
            info.setId(infoDto.getId());
            info.setName(infoDto.getName());
            info.setStatus(UP_STATUS);
            info.setOrigin(infoDto.getOrigin());
            info.setTypeId(infoDto.getTypeId());
            updateById(info);
        }
        return Result.ok("成功");

    }

    public Result uporoff(String id) {
        Info info=infoMapper.selectById(id);
        info.setStatus(info.getStatus().equals(UP_STATUS) ? OFF_STATUS :UP_STATUS);
        infoMapper.updateById(info);
        return Result.ok("成功");
    }
    public Result coffeetop4(){
        List<Info> list=infoMapper.coffeetop4();
        return Result.ok(list);
    }
}
